import time

import numpy as np

from PRNG import PRNG_Cplx, PRNG_Cplx_T
from complex_PLM.chart import showChart, showChart_2, ss_chart

S1 = [
    [0x55, 0xC2, 0x63, 0x71, 0x3B, 0xC8, 0x47, 0x86, 0x9F, 0x3C, 0xDA, 0x5B, 0x29, 0xAA, 0xFD, 0x77],
    [0x8C, 0xC5, 0x94, 0x0C, 0xA6, 0x1A, 0x13, 0x00, 0xE3, 0xA8, 0x16, 0x72, 0x40, 0xF9, 0xF8, 0x42],
    [0x44, 0x26, 0x68, 0x96, 0x81, 0xD9, 0x45, 0x3E, 0x10, 0x76, 0xC6, 0xA7, 0x8B, 0x39, 0x43, 0xE1],
    [0x3A, 0xB5, 0x56, 0x2A, 0xC0, 0x6D, 0xB3, 0x05, 0x22, 0x66, 0xBF, 0xDC, 0x0B, 0xFA, 0x62, 0x48],
    [0xDD, 0x20, 0x11, 0x06, 0x36, 0xC9, 0xC1, 0xCF, 0xF6, 0x27, 0x52, 0xBB, 0x69, 0xF5, 0xD4, 0x87],
    [0x7F, 0x84, 0x4C, 0xD2, 0x9C, 0x57, 0xA4, 0xBC, 0x4F, 0x9A, 0xDF, 0xFE, 0xD6, 0x8D, 0x7A, 0xEB],
    [0x2B, 0x53, 0xD8, 0x5C, 0xA1, 0x14, 0x17, 0xFB, 0x23, 0xD5, 0x7D, 0x30, 0x67, 0x73, 0x08, 0x09],
    [0xEE, 0xB7, 0x70, 0x3F, 0x61, 0xB2, 0x19, 0x8E, 0x4E, 0xE5, 0x4B, 0x93, 0x8F, 0x5D, 0xDB, 0xA9],
    [0xAD, 0xF1, 0xAE, 0x2E, 0xCB, 0x0D, 0xFC, 0xF4, 0x2D, 0x46, 0x6E, 0x1D, 0x97, 0xE8, 0xD1, 0xE9],
    [0x4D, 0x37, 0xA5, 0x75, 0x5E, 0x83, 0x9E, 0xAB, 0x82, 0x9D, 0xB9, 0x1C, 0xE0, 0xCD, 0x49, 0x89],
    [0x01, 0xB6, 0xBD, 0x58, 0x24, 0xA2, 0x5F, 0x38, 0x78, 0x99, 0x15, 0x90, 0x50, 0xB8, 0x95, 0xE4],
    [0xD0, 0x91, 0xC7, 0xCE, 0xED, 0x0F, 0xB4, 0x6F, 0xA0, 0xCC, 0xF0, 0x02, 0x4A, 0x79, 0xC3, 0xDE],
    [0xA3, 0xEF, 0xEA, 0x51, 0xE6, 0x6B, 0x18, 0xEC, 0x1B, 0x2C, 0x80, 0xF7, 0x74, 0xE7, 0xFF, 0x21],
    [0x5A, 0x6A, 0x54, 0x1E, 0x41, 0x31, 0x92, 0x35, 0xC4, 0x33, 0x07, 0x0A, 0xBA, 0x7E, 0x0E, 0x34],
    [0x88, 0xB1, 0x98, 0x7C, 0xF3, 0x3D, 0x60, 0x6C, 0x7B, 0xCA, 0xD3, 0x1F, 0x32, 0x65, 0x04, 0x28],
    [0x64, 0xBE, 0x85, 0x9B, 0x2F, 0x59, 0x8A, 0xD7, 0xB0, 0x25, 0xAC, 0xAF, 0x12, 0x03, 0xE2, 0xF2]
]

def decimal_to_binary(decimal):
    binary = ""
    while decimal != 0:
        decimal *= 2
        if decimal >= 1:
            binary += "1"
            decimal -= 1
        else:
            binary += "0"
        if len(binary) > 32:  # 设置精度为32位，防止死循环
            break
    return binary

def save_string_array_to_txt(string_array, filename):
    with open(filename, 'w') as file:
        for item in string_array:
            file.write(item)


def calculate_average_absolute_difference(seq1, seq2):
    # 序列长度
    length = len(seq1)

    # 确保两个序列长度一致
    if length != len(seq2):
        raise ValueError("Sequence lengths do not match.")

    # 计算绝对差值，并求和
    total_diff = sum(abs(seq1[i] - seq2[i]) for i in range(length))

    # 计算平均绝对差
    average_diff = total_diff / length

    return average_diff


def calculate_ideal_average_absolute_difference(sequence1, sequence2):
    # 确保序列长度一致
    if len(sequence1) != len(sequence2):
        raise ValueError("Sequence lengths do not match.")

    # 计算差异值
    diffs = np.abs(np.array(sequence1) - np.array(sequence2))

    # 计算理想平均绝对差
    ideal_avg_diff = np.mean(diffs)

    return ideal_avg_diff


if __name__ == '__main__':
    start_time = time.time()
    # key = "this is a key n"  # 密钥key
    x0 = 0.13 #初始状态
    u1 = 4  # 迭代系统参数u1->(0,4)
    n1 = 64  # 映射的分段总数
    y0 = 0.36
    u2 = 0.0000000002  # 迭代系统参数u2->(0,4)
    n2 = 64  # 映射的分段总数

    is_int_s = True  # 是否输出整数序列

    stream_len = 50000  # key_length：生成的密钥长度，单位为字节，密钥流位数为8*key_length

    for i in range(5):
        key_stream_1 = PRNG_Cplx_T(x0, y0, n1, n2, u1, u2, stream_len, is_int_s)
        key_stream_2 = PRNG_Cplx_T(x0 + 0.01, y0, n1, n2, u1, u2, stream_len, is_int_s)
        # diff_sequence = np.bitwise_xor(key_stream_1, key_stream_2)  # 计算差分序列
        # diff_count = np.count_nonzero(diff_sequence)  # 统计差分次数
        x0 += 0.01
        # average_diff_prob = diff_count / len(diff_sequence)  # 计算平均差分概率
        # print("第" + str(i) + "平均差分概率：", diff_count)
        avg_diff = calculate_average_absolute_difference(key_stream_1, key_stream_2)
        print("平均绝对差:", avg_diff)
    avg_diff_t = calculate_ideal_average_absolute_difference(key_stream_1, key_stream_2)
    print("理想平均绝对差:", avg_diff_t)
    # print(key_stream_1)



    # end_time = time.time()
    # execution_time = end_time - start_time
    #
    # s_stream = [decimal_to_binary(i) for i in key_stream]
    #
    # s_stream = [S1[i >> 4][i & 0x0F] for i in key_stream]
    # high_nibble = data >> 4
    # low_nibble = data & 0x0F
    # print(hex_array[2])
    # print(hex_array[2] >> 4)
    # print(hex_array[2] & 0x0F)
    # print(S1[1][1])
    # print(s_stream)
    # save_string_array_to_txt(s_stream, "output.txt")

    # # 输出执行时间
    # print("执行时间：", execution_time, "秒")
    # if is_int_s:
    #     showChart(key_stream)
    # else:
    #     showChart_2(key_stream)
        # ss_chart(stream_len, key_stream)
